class AppController < ApplicationController
  def app
    @remote_ip = request.remote_ip

    @username_valid = false
    @seed_valid = false

    @seed = Seed.new
    old_seed = cookies[:s] || false
    @seed.get_seed
    #TODO: Error-checking
    @seed.save

    if cookies[:un] and (users = User.all un: cookies[:un]).length > 0
     @username_valid = true
     user = users[0]
     if cookies[:s] and user.rp[@remote_ip] == cookies[:s]
       @seed_valid = true
       cookies[:s] = @seed
       user.rp[@remote_ip] = @seed
     end
    end

    oseed = Seed.all(s: old_seed).first
    oseed.destroy if oseed

    cookies[:s] = @seed.s
  end
end
